package com.test.sdk.core.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.test.sdk.common.exception.SdkException;
import com.test.sdk.common.util.*;
import com.test.sdk.core.cache.CPCache;
import com.test.sdk.service.OnlineService;
import com.test.sdk.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.URLDecoder;
import java.util.concurrent.TimeUnit;

/**
 * @author 徒有琴
 */
@RestController
public class UserController {
    @Reference
    private UserService userService;

    @Reference
    private OnlineService onlineService;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @RequestMapping("/common/login.html")
    //@ResponseBody
    public ResponseTO regist(String account, String password, Integer cpId) throws SdkException {
        if (StringUtils.isEmpty(account) || StringUtils.isEmpty(password)) {
            throw new SdkException(ErrorConstants.PARAM_ERROR);
        }
        try {
            password = URLDecoder.decode(password, "utf-8");
            //前端传来的密码是加密后的密文  需要解密解密
            password = DESUtil.decode(password, Base64.encode(CPCache.getCPById(cpId).getSecret().getBytes()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (!AccountUtil.checkPassword(password)) {
            throw new SdkException(ErrorConstants.PASSWORD_INVALID);
        }
        System.out.println(password);
        String ticket = userService.doLogin(account, password);
        //加密ticket 传给前端
        ticket = TicketUtil.encode(ticket);
        ResponseTO response = new ResponseTO();
        response.setTicket(ticket);
        return response;
    }

    @RequestMapping("/user/heartbeat.html")
    public ResponseTO heartbeat(String ticket) {
        ticket = TicketUtil.decode(ticket);
        onlineService.updateLastAct(ticket);
        redisTemplate.expire(ticket, 20, TimeUnit.MINUTES);
        ResponseTO response = new ResponseTO();
        response.setTicket(TicketUtil.encode(ticket));
        return response;
    }

    @RequestMapping("/common/regist.html")
    //@ResponseBody
    public ResponseTO regist(String account, String password, String code, Integer cpId) throws SdkException {
        try {
            System.out.println("解密前" + password);
            password = URLDecoder.decode(password, "utf-8");
            //前端传来的密码是加密后的密文  需要解密解密
            password = DESUtil.decode(password, Base64.encode(CPCache.getCPById(cpId).getSecret().getBytes()));
            System.out.println(password);
        } catch (Exception e) {
            e.printStackTrace();
            throw new SdkException(ErrorConstants.PASSWORD_INVALID);
        }
        int accountType = AccountUtil.getAccountType(account);
        if (accountType == SdkConstants.ACCOUNT_TYPE_UNKNOWN) {
            throw new SdkException(ErrorConstants.ACCOUNT_INVALID);
        }
        if (!AccountUtil.checkPassword(password)) {
            throw new SdkException(ErrorConstants.PASSWORD_INVALID);
        }
        if (accountType == SdkConstants.ACCOUNT_TYPE_NUM) {
            if (StringUtils.isEmpty(code)) {
                throw new SdkException(ErrorConstants.CODE_EMPTY);
            }
            userService.doNumRegist(account, code, password);
        } else {
            userService.doNameRegist(account, password);
        }

        return new ResponseTO("注册成功");
    }
}
